﻿Public Class ToolStrip : Inherits Windows.Forms.ToolStrip

	Sub New()
		MyBase.New()
		Renderer = New Renderer
		Padding = New Padding(0, 0, 1, 3)
		Margin = New Padding(0)
		GripStyle = ToolStripGripStyle.Hidden
		DoubleBuffered = True
	End Sub

	Property AlwaysFlat As Boolean = True

	Protected Overrides Sub OnItemAdded(e As System.Windows.Forms.ToolStripItemEventArgs)
		MyBase.OnItemAdded(e)
		If TypeOf e.Item Is ToolStripControlHost Then Return
		If TypeOf e.Item Is ToolStripSeparator Then Return
		e.Item.Padding = New Padding(2, 1, 2, 1)
		e.Item.Margin = New Padding(0, 1, 0, 1)
	End Sub

	Function AddButton(Name As String, Text As String, Optional Image As Image = Nothing) As ToolStripItem
		Dim RightAlign As Boolean
		If Name.StartsWith("-") Then
			Dim Sep = Items.Add("-")
			Dim Added = AddButton(Mid(Name, 2, Name.Length - 1), Text, Image)
			Sep.Alignment = Added.Alignment
			Return Added
		ElseIf Name.EndsWith("-") Then
			Dim Added = AddButton(Mid(Name, 1, Name.Length - 1), Text, Image)
			Dim Sep = Items.Add("-")
			Sep.Alignment = Added.Alignment
			Return Added
		End If
		If Name.EndsWith(">") Then
			Name = Name.Replace(">", "")
			RightAlign = True
		End If
		If Name = "-" Then
			Items.Add("-")
			Return Nothing
		ElseIf Name = "label" Then
			Dim Label As New ToolStripLabel
			Label.Text = Text
			Items.Add(Label)
			Return Label
		ElseIf Name.StartsWith("^") Then
			Name = Mid(Name, 2)
			Dim Button As New ToolStripSplitButton With {.Name = Name, .Text = Text, .Image = Image}
			Items.Add(Button)
			Return Button
		ElseIf Name.StartsWith("+") Then
			Name = Mid(Name, 2)
			Dim Button As New ToolStripDropDownButton With {.Name = Name, .Text = Text, .Image = Image}
			Items.Add(Button)
			Return Button
		Else
			Dim Button As New ToolStripButton With {.Name = Name, .Text = Text, .Image = Image}
			If Text = "" Then Button.DisplayStyle = ToolStripItemDisplayStyle.Image
			If Text <> "" Then Button.AutoToolTip = False
			Items.Add(Button)
			If RightAlign Then Button.Alignment = ToolStripItemAlignment.Right
			Visible = True
			Return Button
		End If
	End Function

	Protected Overrides Sub OnVisibleChanged(e As System.EventArgs)
		MyBase.OnVisibleChanged(e)
		OnSizeChanged(Nothing)
	End Sub

	Protected Overrides Sub OnSizeChanged(e As System.EventArgs)
		For Each Item As ToolStripItem In Items
			If TypeOf Item Is ToolStripSpring Then
				Dim Width = Parent.Width
				For Each b As ToolStripItem In Items
					If b.Visible AndAlso Not TypeOf b Is ToolStripSpring Then
						Width -= b.Width + b.Margin.Horizontal
					End If
				Next
				Width -= Padding.Horizontal + Item.Margin.Horizontal
				Item.Width = Width
				Item.Height = 26
				Exit For
			End If
		Next
		MyBase.OnSizeChanged(e)
	End Sub

	Function AddButtonMenu(Parent As String, Name As String, Text As String, Optional Image As Image = Nothing, Optional ShortcutKeys As Keys = Nothing) As ToolStripMenuItem
		Dim ParentButton As ToolStripDropDownItem = Items(Parent)
		If Name = "-" Or Text = "-" Then
			ParentButton.DropDownItems.Add("-")
			Return Nothing
		End If
		Dim Menu As New ToolStripMenuItem With {.Name = Parent & "." & Name, .Text = Text, .Image = Image, .Padding = New Padding(0, 2, 0, 1)}
		Menu.ShortcutKeys = ShortcutKeys
		ParentButton.DropDownItems.Add(Menu)
		Return Menu
	End Function

	Function AddControl(Name As String, Control As Control) As ToolStripControlHost
		Dim ControlHost As New ToolStripControlHost(Control)
		Items.Add(ControlHost)
		Return ControlHost
	End Function

	ReadOnly Property Process() As Process
		Get
			Dim Obj As Object = Me
			Do Until TypeOf Obj Is ProcessBase
				If Obj Is Nothing Then Return Nothing
				Obj = Obj.Parent
			Loop
			Return Obj
		End Get
	End Property

	Sub Rearrange()
		OnSizeChanged(Nothing)
	End Sub

	Private Sub ToolStrip_ItemClicked(sender As Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles Me.ItemClicked
		If TypeOf e.ClickedItem Is ToolStripButton Then
			Dim Button As ToolStripButton = e.ClickedItem
			Dim Group = (From g In Groups From b In g.Value Where g.Value.Contains(Button) Select g.Value).FirstOrDefault
			If Not Group Is Nothing Then
				For Each Item In Group
					Item.Checked = Item Is e.ClickedItem
				Next
			End If
		End If
		If Process Is Nothing Then Return
		Process.FireEvent(EventType.ButtonClicked, e.ClickedItem.Name)
	End Sub

	Sub AddButtonGroup(Group As String, Name As String, Text As String, Optional Image As Image = Nothing)
		Dim Button = AddButton(Name, Text, Image)
		If Not Groups.ContainsKey(Group) Then
			Groups.Add(Group, New List(Of ToolStripButton))
		End If
		Groups(Group).Add(Button)
	End Sub

	Dim Groups As New SortedList(Of String, List(Of ToolStripButton))

	Sub GroupSwitch(Group As String, Forward As Boolean)
		Dim Buttons = Groups(Group)
		If Not Buttons.Any Then Return
		Dim Button = (From b In Buttons Where b.Checked).FirstOrDefault
		If Button Is Nothing Then
			Buttons.First.PerformClick()
			Return
		End If
		Dim i = (Buttons.IndexOf(Button) + If(Forward, 1, -1) + Buttons.Count) Mod Buttons.Count
		Buttons(i).PerformClick()
	End Sub

	Sub AddSeperator()
		Items.Add("-")
	End Sub

End Class

Public Class ToolStripSpring : Inherits ToolStripControlHost

	Sub New(c As Control)
		MyBase.new(c)
		AutoSize = False
		Height = 32
		Padding = Padding.Empty
		Margin = Padding.Empty
	End Sub

End Class